package cn.hdu.charge.service;

import cn.hdu.charge.dao.RecordDao;
import cn.hdu.charge.domain.Record;
import cn.hdu.charge.domain.User;
import cn.hdu.utils.commons.CommonUtils;
import com.google.gson.Gson;

import java.sql.SQLException;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class RecordService {

    private RecordDao recordDao = new RecordDao();

    public Object startCharge(Record record) {
        try {
            record.setRecordId(CommonUtils.time());
            record.setStatus(1);
            recordDao.addRecord(record);
            return (new Gson()).toJson(record);
        } catch(SQLException e) {
            return -1;
        }
    }

    public Object endCharge(Record record) {
        try {
            //TODO：向客户端发送推送消息终止
            return recordDao.updateStatus(record.getRecordId());
        } catch(SQLException e) {
            return -1;
        }
    }

    public List<Record> getRecord(User user) {
        try {
            return recordDao.getRecord(user);
        } catch(SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public void Push() {


    }

}
